//
//  ZLWLoginController.m
//  EPA
//
//  Created by lanou3g on 16/4/25.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "ZLWLoginController.h"
#import "ZLWUserInfoManager.h"
#import "ZLWRegisterController.h"
#import "ZLWHomePageController.h"
#import "CK_MenuViewController.h"
@interface ZLWLoginController ()
@property (weak, nonatomic) IBOutlet UITextField *EmailTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *PassWordTextFiled;

@end

@implementation ZLWLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)LoginActioin:(UIButton *)sender {
    NSURL *url = [NSURL URLWithString:@"http://api2.pianke.me/user/login"];
    NSMutableURLRequest *mRequst = [NSMutableURLRequest requestWithURL:url];
    [mRequst setHTTPMethod:@"POST"];
    //设置请求体
    NSString *bodyStr = [NSString stringWithFormat:@"email=%@&passwd=%@",self.EmailTextFiled.text,self.PassWordTextFiled.text];
    NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [mRequst setHTTPBody:body];
    //进行网络请求
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:mRequst completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSLog(@"%@",jsonDict);
            NSString *msg = jsonDict[@"data"][@"msg"];
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:msg?msg : @"登录成功"  preferredStyle:(UIAlertControllerStyleAlert)];
            [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                if ([jsonDict[@"result"] isEqual:@1]) {
//                    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//                    
//                    UINavigationController *detailist = [main instantiateViewControllerWithIdentifier:@"HomePage"];
//                    [[UIApplication sharedApplication].keyWindow setRootViewController:detailist];
                    
                    //ZLWHomePageController *pageVC = [[ZLWHomePageController alloc] init];
                    [self dismissViewControllerAnimated:YES completion:nil];
                    
                }
                
            }]];
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alertVC animated:YES completion:nil];
            });
            if ([jsonDict[@"result"]  isEqual: @1]) {
                [[ZLWUserInfoManager shareManager] LoginWithUserDict:jsonDict[@"data"]];
                NSLog(@"%@",[ZLWUserInfoManager shareManager].auth);
            }
        }
    }];
    [dataTask resume];
    


}
- (IBAction)RegisterAction:(UIButton *)sender {
    
    ZLWRegisterController *RegistrVC = [[ZLWRegisterController alloc] init];
    [self presentViewController:RegistrVC animated:YES completion:nil];
    
    
//    ZLWRegisterController *RegisterVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MinRegister"];
//    [self.navigationController pushViewController:RegisterVC animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
